//
//  ForgetPassWordViewController.m
//  wangzhe
//
//  Created by 李 佳伟 on 2017/10/17.
//  Copyright © 2017年 李 佳伟. All rights reserved.
//

#import "ForgetPassWordViewController.h"
#import "RegisterVIew.h"
#import "JTCSM.h"
#import "ForGetPWNextViewController.h"
#define KTop 10
#define KWidthOfRegister 40

@interface ForgetPassWordViewController ()<UITextFieldDelegate,UIAlertViewDelegate>
{
    NSArray *imageNameArray;
    NSArray *placeholderArray;
    NSString *phoeNumber ;
    NSString *passWord ;
    NSString *passWord1 ;
    NSString *code ;
    NSMutableArray* _areaArray;
    RegisterVIew * registerV;
    UIButton *getKeyBtn;
    NSTimer* _timer1;
    NSTimer* _timer2;
    NSInteger count ;
    UIButton *radioBtn;
    BOOL isSelector;
    NSMutableArray *textFieldArray;
    int idx ;

}
@end

@implementation ForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor =RGBACOLOR(241, 241, 241, 1);
    self.navigationController.navigationBar.hidden = NO;
    textFieldArray = [NSMutableArray arrayWithCapacity:2];
    //设置barbuttonitem
    leftBarButtonItems_rightBarButtonItems *leftbtn = KCUSTOMNAVIGSTION_ALLOC;
    self.navigationItem.leftBarButtonItem = [leftbtn setBarButtonItemsImageName:@"fanhui.png" frame:KLeftBarButtonItemFrame_image target:self SEL:@selector(gotBack)];
    //回收键盘
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeKeyBoard)];
    [self.view addGestureRecognizer:tap];
    [self aboutDataArray];
    [self layoutView];

    // Do any additional setup after loading the view.
}
- (void)gotBack{
    
    self.navigationController.navigationBar.hidden = !self.isShowNav;

    [self.navigationController popViewControllerAnimated:YES];
}


- (void)layoutView{
    
    UIView *backView = [[UIView alloc]init];
    backView.userInteractionEnabled = YES;
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    idx = 0;
    placeholderArray=@[@"输入11位电话号码",@"请输入验证码"];
    for (int i = 0; i < placeholderArray.count; i++) {
        registerV = [[RegisterVIew alloc]initWithFrame:CGRectMake(0,10+i*60, self.view.frame.size.width, KWidthOfRegister+20) labeTtext:nil placeholder:placeholderArray[i]];
        registerV.userInteractionEnabled =YES;
        registerV.textField.textAlignment = NSTextAlignmentLeft;
        registerV.textField.delegate = self;
        registerV.textField.tag = 100+i;
        [registerV.textField setValue:RGBACOLOR(120, 130, 140,1) forKeyPath:@"_placeholderLabel.textColor"];
        registerV.label.textAlignment = NSTextAlignmentLeft;
        [backView addSubview:registerV];
        [textFieldArray addObject:registerV];
        
        registerV.textField.frame = CGRectMake(20, 0,registerV.textField.LC_width, registerV.textField.LC_height);
        //获取数字键盘
        [self getKeyboardPodWithTextfield:registerV.textField];
        if (i ==1) {
            getKeyBtn = [UIButton initWithFrame:CGRectMake(KCONTROLLERWith-KBtnWidth-13, registerV.frame.origin.y+15, KBtnWidth-5, 30) backgrandColor:nil titleColor:RGBACOLOR(89, 89, 89, 1) title:@"获取验证码" titleFont:12];
            [getKeyBtn addTarget:self action:@selector(getToKey) forControlEvents:UIControlEventTouchUpInside];
            [backView addSubview:getKeyBtn];
            [df layerWithView:getKeyBtn radius:getKeyBtn.LC_height/2 width:1 color:KZhuTiRGB];
            registerV.textField.frame = CGRectMake(registerV.textField.frame.origin.x, registerV.textField.frame.origin.y, registerV.textField.frame.size.width-KBtnWidth-10, registerV.textField.frame.size.height);
            backView.frame = CGRectMake(0, KagainChangeHeight, self.view.frame.size.width, registerV.frame.origin.y + registerV.frame.size.height);
        }else{
            [self addLineWith:registerV];
        }
        
    }
    UIButton *doneBtn = [UIButton initWithFrame:CGRectMake(20, KVIEW_Y(backView)+40 , KCONTROLLERWith-40, 50) backgrandColor:KZhuTiRGB titleColor:[UIColor whiteColor] title:@"提   交" titleFont:14];
    [doneBtn addTarget:self action:@selector(goToCompleteVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:doneBtn];
    [df layerWithView:doneBtn radius:25 width:0 color:nil];
    
}

- (void)goToCompleteVC{
    [self.view endEditing:YES];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
    LoadData *request = [[LoadData alloc]init];
    [dict setObject:phoeNumber==nil?@"":phoeNumber forKey:@"mobile"];
    [dict setObject:code==nil?@"":code forKey:@"code"];
    NSLog(@"%@",dict);
    
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"PassPort/smscheck") withDict:dict superView:self.view isAddtag:NO  Block:^(id result) {
        
       
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"status"]];
        if ([isreturn isEqualToString:@"1"]) {
            
            ForGetPWNextViewController *VC = [[ForGetPWNextViewController alloc]init];
            VC.phoneNumber = phoeNumber;
            VC.code = code;
            VC.nameTitle = @"忘记密码";
            [self.navigationController pushViewController:VC animated:YES];
            
        }else{
            [df showAlertWithTitle:[result objectForKey:@"msg"] message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
            //            [self theRequestOftuwenyanzhengma];
        }
        
    }];

   
}
#pragma mark --重新获取验证码
- (void)getTheCode{
    [self.view endEditing:YES];
    
    LoadData *request = [[LoadData alloc]init];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:2];
    [dict setObject:@"find_pass" forKey:@"do"];
    NSLog(@"%@",phoeNumber);
    [dict setObject:phoeNumber forKey:@"mobile"];
    
    [request loadDataPostAsynchronousWithURL:KURLPOSTHeadr(@"PassPort/smscode") withDict:dict superView:self.view isAddtag:NO Block:^(id result) {
        
        [self gettime];
        
        NSLog(@"*********************%@",result);
        if (result == nil ) {
            [df showAlertWithTitle:@"获取验证码失败" message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
            return ;
        }
        NSString *isreturn = [NSString stringWithFormat:@"%@",[result objectForKey:@"result"]];
        if ([isreturn isEqualToString:@"1"]) {
            [df showAlertWithTitle:[result objectForKey:@"msg"] message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
        }else{
            [df showAlertWithTitle:[result objectForKey:@"msg"] message:nil btn1:@"返回" btn2:nil btn3:nil delegate:nil tag:0];
        }
        [getKeyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        
    }];
    
    
}
- (void)getToKey{
    
    [self getTheCode];
    
}



#pragma mark --添加灰线条
- (void)addLineWith:(UIView *)fatherview{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, fatherview.frame.size.height-1, KCONTROLLERWith-40, 1.5)];
    view.backgroundColor = RGBACOLOR(241, 241, 241,1);
    [fatherview addSubview:view];
    
}

- (void)getKeyboardPodWithTextfield:(UITextField *)textfield{
    if (textfield.tag == 100 || textfield.tag == 101 ) {
        textfield.keyboardType = UIKeyboardTypePhonePad;
    }
//    if (textfield.tag == 101||textfield.tag == 102) {
//        textfield.secureTextEntry = YES;
//    }
    
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if (textField.tag == 100) {
        phoeNumber = textField.text;
    }
    if (textField.tag == 101) {
        code = textField.text;
    }
//    if (textField.tag == 101) {
//        passWord = textField.text;
//    }
//    if (textField.tag == 102) {
//        passWord1 = textField.text;
//    }
    
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder] ;
    return YES;
}
- (void)removeKeyBoard{
    [self.view endEditing:YES];
}
#pragma mark --定时器
- (void)gettime{
    [_timer2 invalidate];
    [_timer1 invalidate];
    
    count = 0;
    
    NSTimer* timer=[NSTimer scheduledTimerWithTimeInterval:60
                                                    target:self
                                                  selector:@selector(showRepeatButton)
                                                  userInfo:nil
                                                   repeats:YES];
    
    NSTimer* timer2=[NSTimer scheduledTimerWithTimeInterval:1
                                                     target:self
                                                   selector:@selector(updateTime)
                                                   userInfo:nil
                                                    repeats:YES];
    _timer1=timer;
    _timer2=timer2;
    
}
-(void)updateTime{
    count++;
    if (count>60)
    {
        [_timer2 invalidate];
        return;
    }
    NSString *timeStr=[NSString stringWithFormat:@"%lus重新获取",(long)60-count];

    [getKeyBtn setTitle:timeStr forState:UIControlStateNormal];
    if(count == 60){
        [getKeyBtn setTitle:@"重新获取" forState:UIControlStateNormal];
    }
    
}
-(void)showRepeatButton{
    
    [_timer1 invalidate];
    return;
}
- (void)aboutDataArray{
    
    imageNameArray =@[@"手机号",@"验证码"];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
